package com.example.simple.vm

import androidx.collection.arrayMapOf
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.LifecycleOwner
import com.example.simple.entity.SimpleEntity
import com.libhttp.HttpManager
import com.libhttp.HttpURL
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * Created on 2022/9/13.
 * @author Joker
 * Des:数据中心
 */

object SimpleVM {

    /**
     * 请求数据处理
     */
    fun reqTest(owner: LifecycleOwner, reqResult: ((code: Int, data: SimpleEntity?, msg: String) -> Unit)? = null) {
        HttpManager.get<SimpleEntity>(owner, "", arrayMapOf(), reqResult)
    }


    /**
     * 合并请求
     */
    fun mergeReq(lifecycleScope: LifecycleCoroutineScope, reqResult: (a: SimpleEntity?, b: SimpleEntity?) -> Unit) {
        lifecycleScope.launch(Dispatchers.Main) {
            val a = HttpManager.getSync<SimpleEntity>(this, "", null)
            val b = HttpManager.getSync<SimpleEntity>(this, "", null)
            val aData = a.await()
            val bData = b.await()
            reqResult.invoke(aData, bData)
        }
    }
}